package com.cbd.dao.impl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cbd.dao.UserDao;
import com.cbd.pojo.User;
@Repository
public class UserDaoImpl implements UserDao {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	@Override
	public User findUserByName(String inputName) {
		Session session =sessionFactory.getCurrentSession();
		Query query =session.createQuery("from User u where u.userName=?");
		query.setParameter(0,inputName);
		return (User)query.uniqueResult();
	}
	
	@Override
	public User findUserByUserID(int userID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from User u where u.userID = ? ");
		query.setParameter(0, userID);
		User user = (User) query.list().get(0);
		return user;
	}
	@Override
	public User login(User user) {

		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from User u where u.userName=? and u.userPWD=?");
		query.setParameter(0, user.getUserName());
		query.setParameter(1, user.getUserPWD());
		
		return (User) query.uniqueResult();
	}

}
